package com.quanyan.runningriding.service.impl;

import com.quanyan.api.APIResponse;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.runningriding.entity.db.TbRunUser;
import com.quanyan.runningriding.entity.db.TbRunUserExample;
import com.quanyan.runningriding.entity.vo.run.resp.MusicOperationType;
import com.quanyan.runningriding.mapper.RunMusicOperationTypeMapper;
import com.quanyan.runningriding.mapper.TbRunUserMapper;
import com.quanyan.runningriding.service.RunMusicOperationTypeService;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by liubin on 2016/12/15.
 */

@Service
public class RunMusicOperationTypeServiceImpl implements RunMusicOperationTypeService{
    @Autowired
    private RunMusicOperationTypeMapper runMusicOperationTypeMapper;

    @Autowired
    private TbRunUserMapper tbRunUserMapper;

    @Override
    public APIResponse<List<MusicOperationType>> musicOperationTypeList(Map<String, Object> paramMap) {
        String userId = MapUtils.getString(paramMap,"userId");
        TbRunUserExample tbRunUserExample = new TbRunUserExample();
        tbRunUserExample.createCriteria().andUserIdEqualTo(userId);
        List<TbRunUser> tbRunUsers = tbRunUserMapper.selectByExample(tbRunUserExample);
        if (tbRunUsers.isEmpty()){
            return APIResponse.returnFail("用户id不存在");
        }
        /**
         * type 1 精选 2 喜欢 3 缓存
         */
        Byte type = MapUtils.getByte(paramMap,"type");
//        Integer pageNum = MapUtils.getInteger(paramMap, "pageNum");
//        Integer pageSize = MapUtils.getInteger(paramMap, "pageSize");
//        if (pageNum == null || pageNum <= 0) {
//            pageNum = 1;
//        }
//        if (pageSize == null || pageSize <= 0) {
//            pageSize=9999;
//        }
//        paramMap.put("pageStart", (pageNum - 1) * pageSize);
//        paramMap.put("pageSize", pageSize);
        paramMap.put("type", type);
        paramMap.put("userId", userId);
        Integer total=runMusicOperationTypeMapper.musicOperationTypeListCount(paramMap)==null?0:runMusicOperationTypeMapper.musicOperationTypeListCount(paramMap);
        List<MusicOperationType> musicOperationTypes = runMusicOperationTypeMapper.musicOperationTypeList(paramMap);
        //获取所有歌单id
        List<Integer> collectionIds = new ArrayList<Integer>();
        if (musicOperationTypes.size() > 0 && musicOperationTypes != null) {
            for (int i = 0; i < musicOperationTypes.size(); i++) {
                collectionIds.add(musicOperationTypes.get(i).getMusicCollectionId());
             }
        }
        paramMap.put("collectionIds", collectionIds);
        Integer total2=runMusicOperationTypeMapper.playNumListCount(paramMap)==null?0:runMusicOperationTypeMapper.playNumListCount(paramMap);
        if(total<=0)
            return APIResponse.returnFail("没有数据");
        if (total != total2) {
            return APIResponse.returnFail("服务器错误");
        }

        List<MusicOperationType> playNumList=runMusicOperationTypeMapper.playNumList(paramMap);
        if (musicOperationTypes.size() > 0 && musicOperationTypes.size() == playNumList.size()) {
            for (int i = 0; i < musicOperationTypes.size(); i++) {
                MusicOperationType musicOperationType=musicOperationTypes.get(i);
                musicOperationType.setListenNumber(playNumList.get(i).getListenNumber());
            }
        }

//            PageObj<List<MusicOperationType>> result = PageObj.create(total, pageNum, pageSize, musicOperationTypes);
//        return APIResponse.returnSuccess(result);
        return APIResponse.returnSuccess(musicOperationTypes);
    }
}
